package chain_token

import (
	"chain_bridge_service/internal/code"
	"chain_bridge_service/internal/pkg/core"
	"chain_bridge_service/internal/services/chain_token"
	"net/http"
)

type createResponse struct {
	Code int         `json:"code"`
	Data interface{} `json:"data"`
	Msg  string      `json:"msg"`
}

type RequestParam struct {
	ChainConfigId string `form:"chainConfigId"`
	UserId        string `form:"userId"`
}

func (h *handler) List() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(RequestParam)
		res := new(createResponse)
		if err := ctx.ShouldBindQuery(&req); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.ParamBindError,
				code.Text(code.ParamBindError)).WithError(err),
			)
			return
		}

		data := new(chain_token.RequestData)
		data.ChainConfigId = req.ChainConfigId
		data.UserId = req.UserId
		chainTokenList, err := h.chainTokenService.List(ctx, data)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.GetProductListError,
				code.Text(code.GetProductListError)).WithError(err),
			)
			return
		}
		res.Code = code.StatusOK
		res.Data = chainTokenList
		res.Msg = code.Text(code.StatusOK)
		ctx.Payload(res)
	}
}

func (h *handler) GetListByConfigId() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(RequestParam)
		res := new(createResponse)
		if err := ctx.ShouldBindQuery(&req); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.ParamBindError,
				code.Text(code.ParamBindError)).WithError(err),
			)
			return
		}

		data := new(chain_token.RequestData)
		data.ChainConfigId = req.ChainConfigId
		chainTokenList, err := h.chainTokenService.GetListByConfigId(ctx, data)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.GetProductListError,
				code.Text(code.GetProductListError)).WithError(err),
			)
			return
		}
		res.Code = code.StatusOK
		res.Data = chainTokenList
		res.Msg = code.Text(code.StatusOK)
		ctx.Payload(res)
	}
}
